package com.echat.messagesender.sender.util;

import org.apache.commons.io.IOUtils;

import java.io.*;

/**
 * Created by feelchat on 2016/3/30.
 * IO操作工具类
 */
public class IOUtil {

    private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;

    private static final int EOF = -1;

    /**
     * InputStream转byte[]
     *
     * @param input
     * @return
     * @throws IOException
     */
    public static byte[] toByteArray(InputStream input) throws IOException {
        return IOUtils.toByteArray(input);
    }

    /**
     * inputStream转string
     *
     * @param inputStream
     * @return
     * @throws IOException
     */
    public static String inputStream2String(InputStream inputStream) throws IOException {
        return IOUtils.toString(inputStream);
    }

    /**
     * byte[]转file
     *
     * @param buf
     * @param fileName
     * @return
     * @throws IOException
     */
    public static File byte2File(byte[] buf, String fileName) throws IOException {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;
        try {
            file = new File(fileName);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(buf);
        } finally {
            if (bos != null) {
                bos.close();
            }
            if (fos != null) {
                fos.close();
            }
        }
        return file;
    }

    /**
     * file转byte[]
     *
     * @param file
     * @return
     * @throws IOException
     */
    public static byte[] fileToByteArray(File file) throws IOException {
        FileInputStream fileInputStream = null;
        byte[] bFile = null;
        try {
            bFile = new byte[(int) file.length()];
            fileInputStream = new FileInputStream(file);
            fileInputStream.read(bFile);
            fileInputStream.close();
        } finally {
            fileInputStream.close();
            bFile.clone();
        }
        return bFile;
    }

    /**
     * 获取amr文件的播放时长
     *
     * @param file
     * @return
     * @throws IOException
     */
    public static long getAmrFileDuration(File file) throws IOException {
        long duration = -1;
        int[] packedSize = {12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0};
        RandomAccessFile randomAccessFile = null;
        try {
            randomAccessFile = new RandomAccessFile(file, "rw");
            long length = file.length();//文件的长度
            int pos = 6;//设置初始位置
            int frameCount = 0;//初始帧数
            int packedPos = -1;
            byte[] datas = new byte[1];//初始数据值
            while (pos <= length) {
                randomAccessFile.seek(pos);
                if (randomAccessFile.read(datas, 0, 1) != 1) {
                    duration = length > 0 ? ((length - 6) / 650) : 0;
                    break;
                }
                packedPos = (datas[0] >> 3) & 0x0F;
                pos += packedSize[packedPos] + 1;
                frameCount++;
            }
            duration += frameCount * 20;//帧数*20
        } finally {
            if (randomAccessFile != null) {
                randomAccessFile.close();
            }
        }
        return duration;
    }

    /**
     * 文件转二进制数组
     *
     * @param file
     * @return
     */
    public static byte[] file2byte(File file) throws Exception {
        FileInputStream fis = null;
        ByteArrayOutputStream bos = null;
        byte[] buffer = null;
        try {
            fis = new FileInputStream(file);
            bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            buffer = bos.toByteArray();
        } finally {
            fis.close();
            bos.close();
        }
        return buffer;
    }

}
